package ar.edu.caece.compiler.grammar.factories;

import ar.edu.caece.compiler.grammar.AbstractTokenVisitorFactory;
import ar.edu.caece.compiler.grammar.GrammarSymbol;
import ar.edu.caece.compiler.grammar.ParsingException;
import ar.edu.caece.compiler.grammar.TokenVisitorInterface;
import ar.edu.caece.compiler.grammar.nonterminals.OTROP;
import ar.edu.caece.compiler.grammar.nonterminals.FINALP1;
import ar.edu.caece.compiler.grammar.terminals.GrammarTerminal;
import ar.edu.caece.compiler.grammar.terminals.ComaToken;

public class FINALP1Factory extends AbstractTokenVisitorFactory {

	private static FINALP1Factory INSTANCE;
	
	private OTROPFactory otropFactory;
	private TokenVisitorInterface parseOTROP;
	
	public static FINALP1Factory getInstance() throws Exception {
		if(INSTANCE==null) INSTANCE = new FINALP1Factory();
		return INSTANCE;
	}
	
	private Initializer initializer = new Initializer() {
		
		@Override
		public void init() throws Exception {
			initializer = NoOperationInitializer.getInstance();
			otropFactory = OTROPFactory.getInstance();
			parseOTROP = otropFactory.makeVisitor();
		}
	};
	
	private FINALP1Factory() throws Exception {

	}
	
	@Override
	public TokenVisitorInterface makeChainedVisitor(
			final TokenVisitorInterface siguiente) throws Exception {
		initializer.init();

		return new ComaToken.ComaChainVisitor(siguiente) {
			
			@Override
			public GrammarSymbol comaCase(ComaToken llamador) throws Exception {
				return new FINALP1(llamador, (OTROP) nextToken().execute(parseOTROP));
			}
		};
	
	}
	@Override
	public TokenVisitorInterface makeVisitor() throws Exception {
		initializer.init();

		return new ComaToken.ComaTokenVisitor() {
			
			@Override
			public GrammarSymbol defaultCase(GrammarTerminal llamador) throws Exception {
				throw new ParsingException("Error de parsing.",FINALP1.class,llamador);
			}
			
			@Override
			public GrammarSymbol comaCase(ComaToken llamador) throws Exception {
				return new FINALP1(llamador, (OTROP) nextToken().execute(parseOTROP));
			}
		};
	}
	
	
}
